package com.websql.yousql.dao;

import com.websql.yousql.annotation.OperationLog;
import com.websql.yousql.annotation.operationLog.ColumnComment;
import com.websql.yousql.entity.ActionLog;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface ActionLogDao {
    public static class OperationLogMapperProvider{
        public String selectAnyTableSQL(Map<String,String> map) {
            return map.get("sql");
        }
    }
    //添加操作日志
    @Insert("INSERT INTO action_log (create_time,user_id,conn_id,database_name,table_name,action,sql_str,ipaddress,content) VALUES (#{log.createTime},#{log.userId},#{log.connId},#{log.databaseName},#{log.tableName},#{log.action},#{log.sqlStr},#{log.ipaddress},#{log.content});")
    @Options(useGeneratedKeys=true,keyColumn="id",keyProperty="id")
    void insertOperationLog(@Param("log") ActionLog actionLog);

    //查询任意sql
    @SelectProvider(type=OperationLogMapperProvider.class,method="selectAnyTableSQL")
    Map<String,Object> selectAnyTable(@Param("sql")String sql);

    //查询任意表的字段与备注
    @Select("SELECT COLUMN_NAME `column`,column_comment `comment` FROM INFORMATION_SCHEMA.Columns WHERE table_name=#{table}")
    List<ColumnComment> selectColumnCommentByTable(@Param("table")String tableName);



}

